﻿Imports Equipment.DataAccess
Imports Equipment.Business
Imports AppUser = Equipment.DataAccess.User
Imports Equipment.Model


Namespace Equipment
    Public Class EmailNotificationController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /EmailNotification

        Function Index() As ActionResult
            Dim profile As AppUser = AppUser.GetByPrimaryKey(Application.CurrentUserInfo.UserID)
            ViewData("Notifications") = UserNotification.RetrieveAll()
            Return View()
        End Function

        <HttpPost()> _
        Function Index(ByVal collection As FormCollection) As ActionResult

            Dim profile As AppUser = AppUser.GetByPrimaryKey(Application.CurrentUserInfo.UserID)

            SaveNotificationPreferences(profile, collection)

            ViewData("Notifications") = UserNotification.RetrieveAllFor(Application.CurrentUserInfo.UserID)

            Return View()
        End Function

        '
        ' GET: /EmailNotification/Create

        Function Create() As ActionResult
            Dim un As New UserNotification()
            Session("currNotification") = un
            ViewData("currNotification") = un
            ViewData("Mode") = "Create"
            ViewData("Trigger") = New SelectList(NotificationTriggerLookup.RetrieveAll(), "Type", "Description")
            Return View()
        End Function

        Function Edit(ByVal id As Integer) As ActionResult
            Dim un As UserNotification = UserNotification.GetByPrimaryKey(id)
            Session("currNotification") = un
            ViewData("currNotification") = un
            ViewData("Mode") = "Edit"
            ViewData("Trigger") = New SelectList(NotificationTriggerLookup.RetrieveAll(), "Type", "Description", DirectCast(un.NotificationTrigger.Value, NotificationTrigger))
            Return View("Create")
        End Function

#Region "AJAX"
        Function DeleteNotification(ByVal id As Integer) As ActionResult
            Dim respResult As New JSONActionResult
            respResult.Result = Result.SUCCESS
            Try
                Dim un As UserNotification = UserNotification.GetByPrimaryKey(id)
                un.Delete()

            Catch ex As Exception
                respResult.Result = Result.ERROR
                respResult.Message = ex.Message
            End Try

            Return Json(respResult)
        End Function
#End Region

#Region "Wizard"
        <HttpPost()> _
        Function SaveType(ByVal trigger As String, ByVal name As String) As ActionResult

            Dim actionResult As New JSONActionResult
            actionResult.Result = Result.SUCCESS

            Try

                Dim formTrigger As NotificationTrigger = [Enum].Parse(GetType(NotificationTrigger), trigger)
                Dim un As UserNotification = Session("currNotification")
                If un.NotificationTrigger <> formTrigger Then
                    un.OverdueType = Nothing
                    un.OverdueXValue = Nothing
                End If

                un.NotificationTrigger = formTrigger
                un.Description = name
                ViewData("currNotification") = un

            Catch ex As Exception
                actionResult.Result = Result.ERROR
                actionResult.Message = ex.Message
            End Try

            Return Json(actionResult)
        End Function


        <HttpPost()> _
        Function OverdueStep() As ActionResult
            Dim un As UserNotification = Session("currNotification")
            ViewData("currNotification") = un

            If un.OverdueType = NotificationOverdueType.EveryXAfterOverdue Then
                ViewData("SelectedOverdueAfterValue") = un.OverdueXValue
            Else
                ViewData("SelectedOverdueBeforeValue") = un.OverdueXValue
            End If

            If un.NotificationTrigger = NotificationTrigger.WorkOrder_Overdue_ByDay Then
                Return PartialView("~/Views/EmailNotification/Controls/OverdueByDay.ascx")
            Else : un.NotificationTrigger = NotificationTrigger.WorkOrder_Overdue_ByHour
                Return PartialView("~/Views/EmailNotification/Controls/OverdueByHour.ascx")
            End If

        End Function

        <HttpPost()> _
        Function DepartmentStep() As ActionResult
            Dim sites As IEnumerable(Of SiteDepartment) = SiteDepartmentManager.GetAllSitesFor(Application.CurrentUserInfo)

            Dim un As UserNotification = Session("currNotification")
            If un.SiteDepartment IsNot Nothing Then
                ViewData("Site") = New SelectList(sites, "ID", "Description", un.SiteDepartment.ParentID)
                Dim locations As IEnumerable(Of SiteDepartment) = SiteDepartmentManager.GetAllDepartmentsBySiteFor(un.SiteDepartment.ParentID, Application.CurrentUserInfo)
                ViewData("Department") = New SelectList(locations, "ID", "Description", un.DepartmentID)
            Else
                ViewData("Site") = New SelectList(sites, "ID", "Description")
                Dim locations As IEnumerable(Of SiteDepartment) = New List(Of SiteDepartment)
                If sites.Count > 0 Then
                    locations = SiteDepartmentManager.GetAllDepartmentsBySiteFor(sites(0).ID, Application.CurrentUserInfo)
                End If
                ViewData("Department") = New SelectList(locations, "ID", "Description")
            End If

            If un.AssetType IsNot Nothing Then
                ViewData("AssetType") = New SelectList(AssetType.RetrieveAll(), "ID", "Description", un.AssetTypeID)
            Else
                ViewData("AssetType") = New SelectList(AssetType.RetrieveAll(), "ID", "Description")
            End If

            ViewData("currNotification") = un

            Return PartialView("~/Views/EmailNotification/Controls/Department.ascx")
        End Function

        <HttpPost()> _
        Function SaveOverdue(ByVal overdueType As String, ByVal overdueXValue As Integer) As JsonResult


            Dim formOverdueType As NotificationOverdueType = [Enum].Parse(GetType(NotificationOverdueType), overdueType)
            Dim actionResult As New JSONActionResult
            actionResult.Result = Result.SUCCESS

            Try
                Dim un As UserNotification = Session("currNotification")
                un.OverdueType = formOverdueType
                un.OverdueXValue = overdueXValue
                ViewData("currNotification") = un
            Catch ex As Exception
                actionResult.Result = Result.ERROR
                actionResult.Message = ex.Message
            End Try

            Return Json(actionResult)
        End Function


        <HttpPost()> _
        Function SaveDepartment(ByVal locationID As Integer?, ByVal assetTypeID As Integer?) As JsonResult

            Dim actionResult As New JSONActionResult
            actionResult.Result = Result.SUCCESS

            Try
                Dim un As UserNotification = Session("currNotification")

                If locationID.HasValue Then
                    'un.DepartmentID = locationID.Value
                    un.SiteDepartment = SiteDepartment.GetByPrimaryKey(locationID.Value)
                End If
                If assetTypeID.HasValue Then
                    'un.AssetTypeID = assetTypeID.Value
                    un.AssetType = AssetType.GetByPrimaryKey(assetTypeID.Value)
                Else
                    un.AssetType = Nothing
                End If
            Catch ex As Exception
                actionResult.Result = Result.ERROR
                actionResult.Message = ex.Message
            End Try

            Return Json(actionResult)
        End Function

        <HttpPost()> _
        Function PreviewStep(ByVal locationID As Integer?, ByVal assetTypeID As Integer?) As ActionResult
            Dim un As UserNotification = Session("currNotification")
            ViewData("currNotification") = un

            Return PartialView("~/Views/EmailNotification/Controls/WizardPreview.ascx")
        End Function


        <HttpPost()> _
        Function Save() As ActionResult
            Dim actionResult As New JSONActionResult
            actionResult.Result = Result.SUCCESS

            Dim un As UserNotification = Session("currNotification")
            Session("currNotification") = un
            ViewData("currNotification") = un

            un.UserID = Application.CurrentUserInfo.UserID

            Try
                If un.ID > 0 Then
                    un.Save()
                Else
                    UserNotification.Insert(un)
                End If

            Catch ex As Exception
                actionResult.Result = Result.ERROR
                actionResult.Message = ex.Message
            End Try

            Return Json(actionResult)
        End Function
#End Region

#Region "Private Section"

        Private Sub InitControls(ByVal selectedNotificationID As Integer, ByVal selectedNotificationName As String, _
                            ByVal selectedNotificationTrigger As NotificationTrigger, ByVal selectedOverdueType As NotificationOverdueType, _
                                    ByVal selectedOverdueXValue As Integer)

            Dim overdueTypes = NotificationOverdueLookup.RetrieveAll()
            For Each overdue In overdueTypes
                If overdue.Type = selectedOverdueType Then
                    overdue.Selected = True
                End If
            Next

            ViewData("ID") = selectedNotificationID
            ViewData("NotificationName") = selectedNotificationName

            If selectedNotificationTrigger = NotificationTrigger.WorkOrder_Overdue_ByDay Or selectedNotificationTrigger = NotificationTrigger.WorkOrder_Overdue_ByHour Then
                ViewData("ShowOverdueTable") = True
            End If

            ViewData("Trigger") = New SelectList(NotificationTriggerLookup.RetrieveAll(), "Type", "Description", selectedNotificationTrigger)
            ViewData("OverdueTypes") = overdueTypes
            ViewData("SelectedOverdueValue") = selectedOverdueXValue

        End Sub

        Private Sub SaveNotificationPreferences(ByVal profile As AppUser, ByVal collection As FormCollection)
            Dim selectedDay As Integer = Integer.Parse(collection("rbDay"))
            profile.Preferences.Clear()
            For Each notificationItem In Notification.RetrieveAll()
                Dim userPreference As New Preference()

                Dim immediately As Boolean = Boolean.Parse(collection(String.Format("cbxSendImmediately_{0}", notificationItem.ID)).Split(",")(0))
                Dim onSelectedDays As Boolean = Boolean.Parse(collection(String.Format("cbxSendOnSelectedDays_{0}", notificationItem.ID)).Split(",")(0))

                userPreference.Immediately = immediately
                userPreference.OnSelectedDay = onSelectedDays
                userPreference.UserID = profile.ID
                userPreference.NotificationID = notificationItem.ID
                profile.Preferences.Add(userPreference)
            Next
            profile.SavePreferences()

            profile.PreferenceDay = selectedDay
            profile.Save()

        End Sub

#End Region

    End Class
End Namespace